package com.nidu.demo.web.system;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.file.api.SysFileService;
import com.nidu.demo.file.dto.FileCO;
import com.nidu.demo.file.dto.FilePageQry;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashSet;
import java.util.List;

/**
 * 文件管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 文件管理")
@RestController
@RequestMapping("/system/file")
@Validated
@RequiredArgsConstructor
public class SysFileController {

    private final SysFileService fileService;

    @Operation(summary = "上传文件", description = "上传文件到默认存储")
    @PostMapping("/upload")
    @PreAuthorize("@ss.hasPermission('system:file:upload')")
    public SingleResponse<FileCO> uploadFile(
            @Parameter(description = "文件", required = true) @RequestParam("file") MultipartFile file,
            @Parameter(description = "模块名称") @RequestParam(value = "module", required = false) String module) {
        return fileService.uploadFile(file, module);
    }

    @Operation(summary = "上传文件到指定存储", description = "上传文件到指定的存储类型")
    @PostMapping("/upload/{storageType}")
    @PreAuthorize("@ss.hasPermission('system:file:upload')")
    public SingleResponse<FileCO> uploadFileToStorage(
            @Parameter(description = "文件", required = true) @RequestParam("file") MultipartFile file,
            @Parameter(description = "模块名称") @RequestParam(value = "module", required = false) String module,
            @Parameter(description = "存储类型", required = true) @PathVariable Integer storageType) {
        return fileService.uploadFile(file, module, storageType);
    }

    @Operation(summary = "删除文件", description = "根据 ID 删除文件")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:file:delete')")
    public Response deleteFile(@PathVariable Long id) {
        return fileService.delete(id);
    }

    @Operation(summary = "批量删除文件", description = "根据 ID 列表删除多个文件")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:file:delete')")
    public Response deleteFiles(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return fileService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取文件详情", description = "根据 ID 获取文件详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:file:query')")
    public SingleResponse<FileCO> getFileById(@PathVariable Long id) {
        return fileService.getById(id);
    }

    @Operation(summary = "分页查询文件", description = "分页获取文件信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:file:query')")
    public PageResponse<FileCO> pageFiles(FilePageQry qry) {
        return fileService.page(qry);
    }
}
